package com.gitee.qdbp.general.system.biz.personnel.convert;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import com.gitee.qdbp.base.system.model.IAccountType;
import com.gitee.qdbp.base.system.service.IUserTypeConverter;

/**
 * 用户类型转换类
 *
 * @author zhh
 * @version 170712
 */
public class UserTypeHandler extends BaseTypeHandler<IAccountType> {

    private IUserTypeConverter converter;

    public UserTypeHandler(IUserTypeConverter converter) {
        this.converter = converter;
    }

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, IAccountType parameter, JdbcType jdbcType)
            throws SQLException {
        ps.setString(i, String.valueOf(parameter.ordinal()));
    }

    @Override
    public IAccountType getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return toUserType(rs.getString(columnName));
    }

    @Override
    public IAccountType getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return toUserType(rs.getString(columnIndex));
    }

    @Override
    public IAccountType getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return toUserType(cs.getString(columnIndex));
    }

    private IAccountType toUserType(String string) {
        return converter.convert(string);
    }
}
